%%登录
-module(fun_login).
-include("common.hrl").
-export([do_init/0,do_close/0,do_call/1,do_info/1,do_time/1,do_msg/1]).

do_init()-> 
	Ptm = lib_protocol:init_pt(),
	Ptm1 = regist_pt(Ptm),
	put(ptm,Ptm1),
	ok.
do_close() -> ok.

do_time(_Time) -> ok.

do_call(_Msg) -> ok.

do_info({timeout, _TimerRef, CallBackInfo}) ->
	case CallBackInfo of
		{Module, Function} ->
			try
				Module:Function()
			catch E:R ->?log_error("timeout error,data=~p,E=~p,R=~p,stack=~p",[CallBackInfo,E,R,erlang:get_stacktrace()])
			end;
		{Module, Function, Args} ->
			try
				Module:Function(Args)
			catch E:R ->?log_error("timeout error,data=~p,E=~p,R=~p,stack=~p",[CallBackInfo,E,R,erlang:get_stacktrace()])
			end;
		_ ->
			?log_warning("unknown timer callback,CallbackInfo=~p", [CallBackInfo])
	end;
do_info(_Info) -> ok.

do_msg({recv, Sid,Ip, Data}) ->
	case lib_protocol:fill_pt(Data, get(ptm)) of
		{ok,Name,Seq,Pt} ->
			process_pt(Name,Seq,Pt,Sid,Ip);
		R -> ?log_error("recv fill_pt error R=~p,Data = ~p",[R,Data])
	end;
do_msg(_Msg) -> ok.

get_pt_list()->
	[
	 pt_login_a001
	].

regist_pt(Ptm) ->
	List = get_pt_list(),
	Fun = fun(Pt,Ptm) -> lib_protocol:regist_pt(Ptm, Pt) end,
	lists:foldl(Fun, Ptm, List).

is_valid_account(Account) ->
	case length(Account) > 2 of
		true ->
			case re:run(Account, "[^a-zA-Z0-9_]") of
				nomatch -> true;
				_ -> false
			end;
		_ -> false
	end.
%%登录游戏
process_pt(pt_login_a001,Seq,Pt,Sid,Ip) -> 
	?debug("pt_login_a001 Pt = ~p,Sid = ~p",[Pt,Sid]),
	Acc = Pt#pt_login.account,
	Psw = Pt#pt_login.password,
	HeadImg = Pt#pt_login.head_img,
	Name = Pt#pt_login.name,
	Sex = Pt#pt_login.sex,
	case is_valid_account(Acc) of
		true ->
			case is_valid_account(Psw) of
				true ->
					case is_ply(Name) of
						true->
							normal_check_acc(Acc,Psw,HeadImg,Name,Sex,Sid,Seq,Ip);
						_->skip
					end;
				_ -> skip
			end;
		_ -> skip
	end;

process_pt(Sort,_Seq,_Pt,_Sid,_Ip) ->
	?debug("pt_not_find:~p",[Sort]),
	ok.
is_ply(Name)->
	case db:dirty_get(ply,Name,#ply.name) of
		[#ply{}|_]->
			false;
		_->true
	end.
normal_check_acc(Acc,Psw,HeadImg,Name,Sex,Sid,Seq,Ip) ->
	?debug("normal_check_acc,{Acc,Psw,Sid,Seq,Ip} = ~p",[{Acc,Psw,HeadImg,Name,Sex,Sid,Seq,Ip}]),
	PswBin = util:to_binary(Psw),
	Now = util:unixtime(),
	case db:dirty_get(account, util:to_binary(Acc),#account.acc_name) of
		[] -> 
			HeadImgNew = case HeadImg of
							 []->"http://img3.duitang.com/uploads/item/201605/13/20160513071115_iTvRQ.png";
							 _->HeadImg
						 end,
			?debug("no acc insert,Acc = ~p",[{Acc}]),
			AccRecord = #account{name= util:to_binary(Name),acc_name = util:to_binary(Acc),acc_psw = util:to_binary(Psw),create_time=Now,last_login_ip=util:ip_to_string(Ip),
								 last_login_time=Now,card=0,login_status=1,head_img = util:to_binary(HeadImgNew),sex=util:to_integer(Sex)},
			case db:insert(AccRecord) of
				[#account{id = Aid}] -> 
					?debug("new acc,Aid = ~p",[{Aid}]),
					case db:insert(#acc_login_log{login_time=Now,logout_time = 0,acc_id = Aid,login_ip=util:to_binary(util:ip_to_string(Ip))}) of
						[#acc_login_log{id = LogID}] -> 
							?debug("new acc,LogID = ~p",[{LogID}]),
							gen_server:cast({global, game_center}, {let_it_in,Aid,Sid,Ip,LogID,Seq});
						_ -> skip
					end;
				_ -> skip
			end;
		[AccRecord = #account{id = Aid,acc_psw = PswBin} | _] -> 
			?debug("find acc,Aid = ~p",[{Aid}]),
			HeadImgNew = case HeadImg of
							 []->"http://img3.duitang.com/uploads/item/201605/13/20160513071115_iTvRQ.png";
							 _->HeadImg
						 end,
			db:dirty_put(AccRecord#account{head_img=HeadImgNew,last_login_ip=util:ip_to_string(Ip),last_login_time=Now,login_status=1}),
			case db:insert(#acc_login_log{login_time=Now,logout_time = 0,acc_id = Aid,login_ip=util:to_binary(util:ip_to_string(Ip))}) of
				[#acc_login_log{id = LogID}] -> 
					?debug("find acc,LogID = ~p",[{LogID}]),
					gen_server:cast({global, game_center}, {let_it_in,Aid,Sid,Ip,LogID,Seq});
				_ -> skip
			end;
		_R -> 
			?debug("login error,_R = ~p",[{_R}]),
			skip
	end.